package com.partners.utils;

import com.partners.configure.wechatmp.WxMpConfiguration;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author liangyongle
 * @date 2019-01-17
 */
public class SharePageUtil {

  public final static String appId = "wx1b38cf3729aaa2a7";


  public static ModelAndView getShareSign(String url, String viewName){
      final WxMpService wxMpService = WxMpConfiguration.getMpServices().get(appId);
      WxJsapiSignature jsapiSignature;
      ModelAndView mav = new ModelAndView();
      mav.setViewName(viewName);
      String  appId = "";
      String nonceStr ="";
      String timestamp = "";
      String signature = "";
      try {
           jsapiSignature = wxMpService.createJsapiSignature(url);
           appId = jsapiSignature.getAppId();
           nonceStr = jsapiSignature.getNonceStr();
           timestamp = String.valueOf(jsapiSignature.getTimestamp());
           signature = jsapiSignature.getSignature();
      } catch (WxErrorException e) {
          //e.printStackTrace();
      }
      mav.addObject("appId",appId);
      mav.addObject("nonceStr",nonceStr);
      mav.addObject("timestamp",timestamp);
      mav.addObject("signature",signature);
      mav.addObject("url",url);

     return mav;
  }


}
